package com.dailyblue.java.net.tcp.demo2;

import lombok.SneakyThrows;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    @SneakyThrows
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ServerSocket ss = new ServerSocket(9999);
        Socket socket = ss.accept();
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        // 说
        while (true) {
            System.out.print("Input:");
            String message = scan.next();
            if ("exit".equals(message)) {
                break;
            }
            out.write(message.getBytes());
        }
        // 读
        byte[] bytes = new byte[10];
        while (true) {
            int len;
            while ((len = in.read(bytes)) != -1) {
                String str = new String(bytes, 0, len);
                System.out.print(str);
            }
            System.out.println("");
        }
    }
}
